package com.riguz.springcloud.api;

import com.fasterxml.jackson.annotation.JsonView;
import com.riguz.springcloud.model.User;
import com.riguz.springcloud.service.UserService;
import com.riguz.springcloud.view.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/ping")
    String ping() {
        return "User Service V1.0";
    }

    @JsonView(View.UserView.class)
    @GetMapping("/user/{id}")
    User getUser(@PathVariable String id) {
        return userService.findUserById(id);
    }

    @JsonView(View.UserView.class)
    @GetMapping("/user")
    List<User> getUsers(@RequestParam(value = "search", required = false) String search,
                        @RequestParam(value = "status", required = false) Integer status) {
        return userService.findUsers(search, status);
    }

    @JsonView(View.UserView.class)
    @PostMapping("/user")
    User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    @JsonView(View.UserView.class)
    @PutMapping("/user/{id}")
    User updateUser(@PathVariable String id, @RequestBody User user) {
        return userService.updateUser(id, user);
    }

    @JsonView(View.UserView.class)
    @DeleteMapping("/user/{id}")
    User deleteUser(@PathVariable String id) {
        return userService.deleteUser(id);
    }

    @JsonView(View.UserView.class)
    @PostMapping("/user/validate")
    User validatePassword(@RequestBody Map<String, String> form) {
        return userService.validatePassword(form.get("name"), form.get("password"));
    }

}
